package cn.ztydata.teachingsystem.heaven.service;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.dao.CourseCodeDao;
import cn.ztydata.teachingsystem.heaven.dao.CourseGroupDao;
import cn.ztydata.teachingsystem.heaven.dao.TeacherDao;
import cn.ztydata.teachingsystem.heaven.dao.TeacherScoreDao;
import cn.ztydata.teachingsystem.heaven.entity.TeacherScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 教师评分业务类
 * Created by wyh on 2015/3/26.
 */
@Service
public class TeacherScoreService extends BaseService {
    @Autowired
    private WebContext webContext;
    @Autowired
    private CourseGroupDao courseGroupDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private TeacherScoreDao teacherScoreDao;
    @Autowired
    private CourseCodeDao courseCodeDao;

    /**
     * 保存评分
     * @param teacherScore 教师评分
     *
     * @author wyh
     * @since 2015-03-26
     */
    @Transactional
    public void saveScore(TeacherScore teacherScore) {
        //判断权限为学生
        if(!(webContext.getUserId().equals(teacherScore.getStudentId()) && webContext.getRole() == Roles.STUDENT)){
            log.debug("新增教师评分时，登录非学生本人：!(webContext.getUserId().equals(teacherScore.getStudentId()) && webContext.getRole() == Roles.STUDENT)");
            log.warn("非法新增：id->{}, role->{}", webContext.getUserId(), webContext.getRole());
            throw new ServiceException("非法新增：没有权限");
        }

        //查询课程代码，教师工号，并判断学生是否在群组中
        StringBuilder courseCode = new StringBuilder();
        StringBuilder teacherNum = new StringBuilder();
        if(!courseGroupDao.checkStudentInGroup(teacherScore.getStudentId(), teacherScore.getGroupId(), courseCode, teacherNum)){
            log.debug("学生不在群组中：!courseGroupDao.checkStudentInGroup(teacherScore.getStudentId(), teacherScore.getGroupId(), courseCode, teacherNum)");
            log.warn("非法新增：学生不在该群组内");
            throw new ServiceException("非法新增：学生不在该群组内");
        }

        //判断课程代码属于这个课程
        if(!courseCodeDao.checkCodeInCourse(courseCode.toString(), teacherScore.getCourseId())){
            log.debug("课程代码不属于该课程：!courseCodeDao.checkCodeInCourse(courseCode.toString(), teacherScore.getCourseId())");
            log.warn("非法新增：群组和课程不相关");
            throw new ServiceException("非法新增：群组和课程不相关");
        }

        //判断对应相同群组、章、学生的数据只有一条
        if(teacherScoreDao.getScore(teacherScore.getGroupId(), teacherScore.getChapterId(), teacherScore.getStudentId()) != null){
            log.debug("对应相同群组、章、学生的数据已存在：teacherScoreDao.getScore(teacherScore.getGroupId(), teacherScore.getChapterId(), teacherScore.getStudentId()) != null");
            log.warn("非法新增：每个用户只能在同一章节下添加一条评分");
            throw new ServiceException("非法新增：每个用户只能在同一章节下添加一条评分");
        }

        teacherScore.setCourseCode(courseCode.toString());
        teacherScore.setTeacherId(teacherDao.getIdByNum(teacherNum.toString()));

        if(teacherScoreDao.saveScore(teacherScore) < 1){
            log.debug("新增教师评分失败：teacherScoreDao.saveScore(teacherScore) < 1");
            throw new ServiceException("新增失败");
        }
    }

    /**
     * 评分预览
     * @param groupId 群组ID
     * @param chapterId 章ID
     * @param studentId 学生ID
     * @return Map
     *
     * @author wyh
     * @since 2015-03-27
     */
    public Map<String, Object> getScore(long groupId, long chapterId, long studentId){
        if(webContext.getUserId() != studentId || webContext.getRole() != Roles.STUDENT){
            log.debug("登录非学生本人：webContext.getUserId() != studentId || webContext.getRole() != Roles.STUDENT");
            throw new ServiceException("无权访问");
        }

        log.debug("返回 教师评分预览");
        return teacherScoreDao.getScore(groupId, chapterId, studentId);
    }
}
